<template>
    <div>
        <el-main>

            <el-breadcrumb separator="/" style="display: inline-block;margin-left: 10px">
                <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item>菜单管理</el-breadcrumb-item>
            </el-breadcrumb>

            <div style="padding: 10px 0">
                <el-input style="width: 200px;margin-left: 5px" placeholder="请输入名称" suffix-icon="el-icon-search" v-model="name"> </el-input>
                <el-button style="margin-left: 5px" type="primary" @click="load">搜索</el-button>
                <el-button style="margin-left: 5px" type="warning" @click="reset">重置</el-button>
            </div>
            <div style="margin: 10px 0">
                <el-button type="primary" @click="newadd">新增 <i class="el-icon-circle-plus-outline"></i></el-button>
                <el-popconfirm
                        confirm-button-text='确定'
                        cancel-button-text='我在想想'
                        icon="el-icon-info"
                        icon-color="red"
                        title="这是一段内容确定删除吗？"
                        @confirm="alldelete"
                >
                    <el-button type="danger"  slot="reference" style="margin: 0px 15px">批量删除 <i class="el-icon-remove-outline"></i></el-button>
                </el-popconfirm>

            </div>

            <el-table :data="tableData" border stripe
                      row-key="id"
                      default-expand-all
                      @selection-change="handleSelectionChange">
                <el-table-column
                        type="selection"
                        width="55">
                </el-table-column>
                <el-table-column prop="id" label="ID" width="80"></el-table-column>
                <el-table-column prop="name" label="名称"></el-table-column>
                <el-table-column prop="path" label="路径"></el-table-column>
                <el-table-column prop="pagePath" label="页面路径"></el-table-column>
                <el-table-column label="图标" class-name="fontSize18" align="center" label-class-name="fontSize12"></el-table-column>
                <el-table-column label="操作"  align="center" width="300">
                    <template slot-scope="scope">
                        <el-button type="primary" @click="handleAdd(scope.row.id)" v-if="!scope.row.pid && !scope.row.path">新增子菜单 <i class="el-icon-plus"></i></el-button>
                        <el-button type="success" @click="update(scope.row)">编辑 <i class="el-icon-edit"></i></el-button>
                        <el-popconfirm
                                confirm-button-text='确定'
                                cancel-button-text='我在想想'
                                icon="el-icon-info"
                                icon-color="red"
                                title="这是一段内容确定删除吗？"
                                @confirm="remove(scope.row.id)"
                        >
                            <el-button type="danger" slot="reference"  style="margin-left: 15px">删除 <i class="el-icon-remove-outline"></i></el-button>
                        </el-popconfirm>

                    </template>
                </el-table-column>
            </el-table>
            <div style="padding: 10px 0">
<!--                <el-pagination-->
<!--                        @size-change="handleSizeChange"-->
<!--                        @current-change="handleCurrentChange"-->
<!--                        :current-page="pageNum"-->
<!--                        :page-sizes="[2, 5, 10]"-->
<!--                        :page-size="pageSize"-->
<!--                        layout="total, sizes, prev, pager, next, jumper"-->
<!--                        :total="total">-->
<!--                </el-pagination>-->

                <el-dialog :title="menutitle" :visible.sync="MenudialogFormVisible" width="30%">
                    <el-form label-width="80px" size="small">
                        <el-form-item label="名称">
                            <el-input v-model="form.name" autocomplete="off"></el-input>
                        </el-form-item>
                        <el-form-item label="路径">
                            <el-input v-model="form.path" autocomplete="off"></el-input>
                        </el-form-item>
                        <el-form-item label="页面路径">
                            <el-input v-model="form.pagePath" autocomplete="off"></el-input>
                        </el-form-item>
                        <el-form-item label="图标">
                            <el-select clearable v-model="form.icon" placeholder="请选择" style="width: 100%">
                                <el-option v-for="item in options" :key="item.name" :label="item.name" :value="item.value">
                                    <i :class="item.value" /> {{ item.name }}
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="顺序">
                            <el-input v-model="form.sortNum" autocomplete="off"></el-input>
                        </el-form-item>
                        <el-form-item label="描述">
                            <el-input v-model="form.description" autocomplete="off"></el-input>
                        </el-form-item>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="MenudialogFormVisible= false">取 消</el-button>
                        <el-button type="primary" @click="Add">确 定</el-button>
                    </div>
                </el-dialog>


                <el-dialog :title="title" :visible.sync="dialogFormVisible" width="30%">
                    <el-form :model="form" >
                        <el-form-item label="名称">
                            <el-input v-model="form.name" autocomplete="off"></el-input>
                        </el-form-item>
                        <el-form-item label="路径">
                            <el-input v-model="form.path" autocomplete="off"></el-input>
                        </el-form-item>
                        <el-form-item label="页面路径">
                            <el-input v-model="form.pagePath" autocomplete="off"></el-input>
                        </el-form-item>
                        <el-form-item label="图标">
                            <el-input v-model="form.icon" autocomplete="off"></el-input>
                        </el-form-item>
                        <el-form-item label="顺序">
                            <el-input v-model="form.sortNum" autocomplete="off"></el-input>
                        </el-form-item>
                        <el-form-item label="描述">
                            <el-input v-model="form.description" autocomplete="off"></el-input>
                        </el-form-item>

                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取 消</el-button>
                        <el-button type="primary" @click="Add">确 定</el-button>
                    </div>
                </el-dialog>

            </div>
        </el-main>
    </div>
</template>

<script>
    import request from "@/utils/request";

    export default {
        name: "menu",
        data(){
            return {
                tableData: [],
                total:0,
                pageSize:8,
                pageNum:1,
                name:"",
                dialogFormVisible:false,
                MenudialogFormVisible:false,
                title:'新增',
                menutitle:'新增菜单',
                form:{},
                multipleSelection: [],
                menuData:[{
                    id: 1,
                    label: '主页',
                    children:[]
                    },
                    {
                        id: 2,
                        label: '系统管理',
                    children: [{
                        id: 3,
                        label: '用户管理',
                    },{
                        id: 4,
                        label: '角色管理',
                    },{
                        id: 5,
                        label: '菜单管理',
                    },{
                        id: 6,
                        label: '文件管理',
                    }]
                }],
            }
        },
        created() {
            this.load()
        },
        methods:{
            handleSizeChange(pageSize){
                this.pageSize=pageSize
                this.load()
            },
            handleCurrentChange(pageNum){
                this.pageNum=pageNum
                this.load()
            },
            load(){
                // request.get("http://localhost:9090/menu/page?pageNum="+this.pageNum+"&pageSize="+this.pageSize+"&name="+this.name).then(res=>{
                //     this.tableData=res.records
                //     this.total=res.total
                // })
                request.get("http://localhost:9090/menu").then(res=>{
                    console.log(res)
                    this.tableData=res
                })

            },
            reset(){
                this.name=""
                this.load()
            },
            newadd(){
                this.title="新增"
                this.dialogFormVisible=true
                this.form={}
            },
            Add(){
                request.post("http://localhost:9090/menu",this.form).then(res=>{
                    if(res){
                        this.$message.success("成功")
                        this.load()
                    }else
                        this.$message.error("失败")
                })
                //this.form=""
                this. MenudialogFormVisible=false
                this. dialogFormVisible=false
            },
            remove(e){
                request.delete("http://localhost:9090/menu/"+e).then(res=>{
                    if(res){
                        this.$message.success("成功")
                        this.load()
                    }else
                        this.$message.error("失败")
                })
            },
            handleSelectionChange(val){
                this.multipleSelection = val;
            },
            alldelete(){
                let ids=this.multipleSelection.map(v=>v.id)
                request.delete("http://localhost:9090/menu/all",{data:ids}).then(res=>{
                    if(res){
                        this.$message.success("批量删除成功")
                        this.load()
                    }else
                        this.$message.error("失败")
                })
            },
            update(e){
                this.title='修改'
                this.form=e
                this.MenudialogFormVisible=true
            },
            handleCheckChange(data, checked, indeterminate) {
                console.log(data, checked, indeterminate);
            },
            handleAdd(e){
                this.MenudialogFormVisible=true
                this.form={}
                if(e){
                    this.form.pid=e
                }
            }
        }
    }
</script>

<style scoped>

</style>